﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Collections;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    [PermissionCheck(false)]
    public partial class SelectWL : BasePage
    {
        private static Hashtable htDW = new Hashtable();
        private static Hashtable htYS = new Hashtable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void BindFirst()
        {
            this.AspNetPager1.PageSize = GridView1.PageSize;
            BindWLLX(this.ddlWLLX, "请选择");
            BindGYS(this.ddlGYS, "请选择");
            using (CS_JBDWBLL jBLL = new CS_JBDWBLL())
            {
                foreach (CS_JBDW item in jBLL.GetAll())
                    htDW[item.BH] = item.MC;
            }
            using (CS_YSBLL yBLL = new CS_YSBLL())
            {
                foreach (CS_YS item in yBLL.GetAll())
                    htYS[item.YSLXBH + "," + item.BH] = item.MC;
            }
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        private void BindData()
        {
            string kcIDs = string.Empty;
            Dictionary<string, string> dc = new Dictionary<string, string>();
            using (KCBLL kBLL = new KCBLL())
            {
                dc.Add("WPLX", EnumHelper.GetDescriptionByName(typeof(EnumKC_LX), EnumKC_LX.WL));
                int RecordCount = 0;
                foreach (var item in kBLL.GetWLKC(dc, true, "BH asc", null, 1000, out RecordCount).Select(t => t.WPID).ToList())
                    kcIDs += "," + item.ToString();
                dc.Clear();
            }
            if (!string.IsNullOrEmpty(kcIDs))
                dc.Add("WPID", kcIDs.Trim(','));
            if (!string.IsNullOrEmpty(txtBH.Text))
                dc.Add("BH", txtBH.Text.Trim());
            if (!string.IsNullOrEmpty(txtName.Text))
                dc.Add("MC", txtName.Text.Trim());
            if (!string.IsNullOrEmpty(ddlWLLX.SelectedValue))
                dc.Add("WLLX", ddlWLLX.SelectedValue);
            if (!string.IsNullOrEmpty(ddlGYS.SelectedValue))
                dc.Add("GYS", ddlGYS.SelectedValue);
            using (ZL_WLBLL zBLL = new ZL_WLBLL())
            {
                int RecordCount = 0;
                List<ZL_WL> itemLists = zBLL.GetWL(dc, "BH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Literal liYS = e.Row.FindControl("liYS") as Literal;
                Literal liDW = e.Row.FindControl("liDW") as Literal;
                Literal liWLLX = e.Row.FindControl("liWLLX") as Literal;
                Literal liGYS = e.Row.FindControl("liGYS") as Literal;
                ZL_WL wl = e.Row.DataItem as ZL_WL;
                liDW.Text = Convert.ToString(htDW[wl.DWBH]);
                liYS.Text = Convert.ToString(htYS[wl.YSLXBH + "," + wl.YSBH]);
                if (ddlWLLX.Items.FindByValue(wl.LXBH) != null)
                    liWLLX.Text = ddlWLLX.Items.FindByValue(wl.LXBH).Text;
                if (ddlGYS.Items.FindByValue(wl.GYSID.ToString()) != null)
                    liGYS.Text = ddlGYS.Items.FindByValue(wl.GYSID.ToString()).Text;
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}